package com.lun.blog.controller.web;

import com.lun.blog.dto.BlogQuery;
import com.lun.blog.pojo.Blog;
import com.lun.blog.pojo.Type;
import com.lun.blog.pojo.User;
import com.lun.blog.repostory.CommentRepository;
import com.lun.blog.service.BlogService;
import com.lun.blog.service.CommentService;
import com.lun.blog.service.TypeService;
import com.lun.blog.service.UserService;
import com.lun.blog.utils.MarkdownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Function : BlogSController
 * @Description:
 * @Param:
 * @version: v1.0.0
 * @author: fanglj5
 * @date : 2021/3/15
 * Modification History:
 * Date Author Version Description
 * -------------------------------------------------------------
 * 时间 作者 版本号 修改原因
 */
@Controller
@RequestMapping("/web/blog")
public class BlogsController {

    @Autowired
    BlogService blogService;

    @Autowired
    UserService userService;

    @Autowired
    TypeService typeService;

    @Autowired
    CommentService commentService;

    @GetMapping("/toBlog")
    public String toBlog(Long blogId, Model model){
        Blog blog = blogService.findById(blogId);
        blog.setView(blog.getView()+1);
        User user = userService.findByUserId(blog.getUser().getId());
        Type type = typeService.findById(blog.getType().getId());
        Long comment = commentService.countComment();
        //将blog中的markdown语法转为html输出
        String context = MarkdownUtils.markdownToHtmlExtensions(blog.getContext());
        BlogQuery query = new BlogQuery();
        query.setId(blogId);
        query.setComments(comment.toString());
        query.setContent(context);
        query.setCreateDate(blog.getCreateTime());
        query.setFlag(blog.getFlag());
        query.setTitle(blog.getTitle());
        query.setUsername(user.getUsername());
        query.setView(blog.getView().toString());
        query.setTypeName(type.getName());
        blogService.saveBlog(blog);
        model.addAttribute("blog",query);
        return "blog";
    }
}
